import axios from 'axios'
import 'nprogress/nprogress.css'
import Nprogress from 'nprogress'
import { getUserTempId } from '@/utils/getUserTempId'
import store from '@/store'

const ajax  = axios.create({
  baseURL:'/api',
  timeout:5000
})
//设置请求拦截器
ajax.interceptors.request.use(config=>{
  config.headers.userTempId = getUserTempId()
  //每次登陆的时候,都应该携带token,所以在请求拦截器中添加token
  const token = store.state.user.userInfo.token
  if(token){
    config.headers.token = token
  }
  Nprogress.start()
  return config
})

//响应拦截器

ajax.interceptors.response.use(result =>{
  Nprogress.done()
return result.data
},err=> {
  Nprogress.done()
 return Promise.reject(err.message)
})
export default ajax